//
//  HRShowSelectSexVIew.m
//  Hear
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowSelectSexView.h"
#import "UIView+YYAdd.h"
@interface HRShowSelectSexView ()
@property (nonatomic, copy)SelectIndexBlock selectIndexBlock;

@end
@implementation HRShowSelectSexView

+(void)showSelectSexView:(SelectIndexBlock)selectIndex
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowSelectSexView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowSelectSexView class]]){
        
        
        HRShowSelectSexView *shareView = (HRShowSelectSexView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:selectIndex];
        
    }
}

- (void)loadView:(SelectIndexBlock)selectIndex
{
    self.selectIndexBlock = selectIndex;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeView)];
    [self addGestureRecognizer:tap];
    
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, kFullHeight+120, kFullWidth, 120)];
    showView.backgroundColor = [UIColor whiteColor];
    [self addSubview:showView];
    [UIView animateWithDuration:0.3 animations:^{
        showView.top = kFullHeight - 120;
    }];
    
    UIButton *continueButton = [UIButton buttonWithType:1];
    continueButton.frame = CGRectMake(0, 0, kFullWidth, showView.height/2);
    [continueButton setTitle:@"男" forState:UIControlStateNormal];
    [continueButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    continueButton.titleLabel.font = CustomUIFont(15);
    [continueButton addTarget:self action:@selector(continueButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    continueButton.tag = 1;
    [showView addSubview:continueButton];
    
    UIButton *againButton = [UIButton buttonWithType:1];
    againButton.frame = CGRectMake(0, continueButton.height, continueButton.width, continueButton.height);
    [againButton setTitle:@"女" forState:UIControlStateNormal];
    [againButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    againButton.titleLabel.font = CustomUIFont(15);
    [againButton addTarget:self action:@selector(continueButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    againButton.tag = 2;
    [showView addSubview:againButton];
}

- (void)continueButtonClick:(UIButton *)sender
{
    [self closeView];
    self.selectIndexBlock(sender.titleLabel.text);
}


-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].top = kFullHeight+120;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
